package formulasimulator;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class SendingThread extends Thread
{
    private boolean clientConnected = true;
    private static final int SOCKET_TIMEOUT = 2000;
    
    private Socket socket;
    
    private String name;
    
    public SendingThread (int counter, Socket client)
    {
        name = "Thread " + counter;
        socket = client;
    }

    @Override
    public void run()
    {
        super.run();
        
        try
        {
            socket.setSoTimeout(SOCKET_TIMEOUT);
            
            // Get the stream to write to it
            PrintStream out = new PrintStream(socket.getOutputStream());
            // InputStream in = socket.getInputStream();

            while (clientConnected)
            {
                System.out.println("\t" + name + ": Reading file raw.bin");
                try (DataInputStream input = new DataInputStream(new FileInputStream("raw.bin")))
                {
                    byte[] buffer = new byte[2048];
                    int limit = 2340;
                    input.skipBytes(1760 * 2048);
                    System.out.println("\t" + name + ": Sending data");
                    while( clientConnected && limit-- > 0 )
                    {
                        // Read data
                        input.read(buffer);

                        // Send packet
                        out.print("OD Data");
                        out.print((char) 10);
                        out.print("Data size: 2048");
                        out.print((char) 10);
                        out.print(buffer);
                        //out.flush();

                        if (limit % 100 == 0)
                        {
                            System.out.println("\t" + name + ": 100 packets sent");
                        }
                        
                        // Sleep to send packets packet each 100 ms
                        try
                        {
                            Thread.sleep(99);
                        }
                        catch (InterruptedException ex)
                        {
                            System.out.println("\t" + name + ": Thread interrupted");
                        }
                            /* Below solution doesn't work
                            try
                            {
                                socket.setSoTimeout(99); // 99ms
                                int value = in.read();
                                socket.setSoTimeout(SOCKET_TIMEOUT);
                                if (value == -1)
                                {
                                    // Client disconnected
                                    System.out.println("Client disconnected");
                                }
                                else
                                {
                                    // Client sent something.. unexpected behaviour, kill client
                                    System.out.println("Unexpected client behaviour. Client sent something. Closing connection");
                                }
                                try { socket.close(); } catch (IOException e) { }
                                clientConnected = false;
                            }
                            catch (SocketTimeoutException ex)
                            {
                                // This exception is expected. Do nothing
                            }
                            //*/
                        
                    }
                    if (limit == 0)
                    {
                        System.out.println("\t" + name + ": File limit reached");
                    }
                }
                catch (EOFException ignored)
                {
                    System.out.println("\t" + name + ": End of file reached");
                }
                catch (FileNotFoundException ignored)
                {
                    System.out.println("\t" + name + ": Input file not found. Closing connection");
                    socket.close();
                    return;
                }
            }
            System.out.println("\t" + name + ": Thread dropped");
        }
        catch (SocketTimeoutException ex)
        {
            System.out.println("\t" + name + ": Client timed out");
            try { socket.close(); } catch (IOException e) { }
        }
        catch (IOException ex)
        {
            System.err.println(ex);
        }
    }
    
    public void drop()
    {
        clientConnected = false;
    }
    
    
}
